let App = getApp();

Page({
  data: {
    list: [],
    options: {},
    default_id: null,
  },

  onLoad: function (options) {

    console.log("options", options);
    // 当前页面参数
    this.setData({
      options: options
    })
  },

  onShow: function () {
    // 获取收货地址列表
    this.getAddressList();
  },

  /**
   * 获取收货地址列表
   */
  getAddressList: function () {
    let _this = this;
    App._post_form('wxapi/address/lists', {}, function (result) {
      console.log("哈哈", result);
      _this.setData({
        list: result.data
      });
    });
  },

  /**
   * 添加新地址
   */
  createAddress: function () {
    wx.navigateTo({
      url: '/pages/address/addAddress/addAddress'
    });
  },

  /**
   * 编辑地址
   */
  editAddress: function (e) {
    wx.navigateTo({
      url: "./detail?address_id=" + e.currentTarget.dataset.id
    });
  },

  /**
   * 移除收货地址
   */
  removeAddress: function (e) {
    let _this = this,
      address_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "您确定要移除当前收货地址吗?",
      success: function (o) {
        o.confirm && App._post_form('wxapi/address/del', {
          address_id: address_id
        }, function (result) {
          _this.getAddressList();
        });
      }
    });
  },
  chooseaddress: function (e) {
    console.log("????", e);
    let _this = this;
    var address_id = e.currentTarget.dataset.id;
    console.log("address_id", address_id);
    _this.setData({
      default_id: parseInt(address_id)
    });

    App._post_form('wxapi/address/setDefault', {
      address_id
    }, function (result) {
      console.log("_this.data.options.from", _this.data.options.from);

      _this.data.options.from === 'flow' && wx.navigateBack();
    });
  },
  /**
   * 设置为默认地址
   */
  setDefault: function (e) {
    console.log("????", e);
    let _this = this,
      address_id = e.detail.value;
    console.log("address_id", address_id);
    _this.setData({
      default_id: parseInt(address_id)
    });

    App._post_form('wxapi/address/setDefault', {
      address_id
    }, function (result) {
      console.log("_this.data.options.from", _this.data.options.from);
      _this.data.options.from === 'flow' && wx.navigateBack();
    });

    return false;
  },

});